const express = require("express");
const router = express.Router();
const permHandler = require("../routes_handle/permission");
const { CODE, Response } = require("../utils/response");
const { checkPermission } = require("../middleware/permission");
const jwt = require("../utils/jwt");

/**
 * =========================
 * 角色管理模块
 * =========================
 */

// 获取角色列表 (POST)
router.post("/admin/role/list", async (req, res, next) => {
  try {
    const { page = 1, pageSize = 10, search } = req.body;
    const roles = await permHandler.getRoleList({ page, pageSize, search });
    res.status(CODE.SUCCESS).json(Response.success(roles));
  } catch (err) {
    next(err);
  }
});
// 获取所有角色列表 (POST)
router.post("/admin/role/all", async (req, res, next) => {
  try {
    const roles = await permHandler.getRoleAll();
    res.status(CODE.SUCCESS).json(Response.success(roles));
  } catch (err) {
    next(err);
  }
});
// 获取角色详情 (GET)
router.get("/admin/role/detail/:id", async (req, res, next) => {
  try {
    const role = await permHandler.getRoleById(req.params.id);
    res.status(CODE.SUCCESS).json(Response.success(role));
  } catch (err) {
    next(err);
  }
});

// 获取角色详情 (POST) —— id 从 body 传
router.post("/admin/role/detail", async (req, res, next) => {
  try {
    const { id } = req.body;
    const role = await permHandler.getRoleById(id);
    res.status(CODE.SUCCESS).json(Response.success(role));
  } catch (err) {
    next(err);
  }
});

// 创建角色 (POST)
router.post(
  "/admin/role/create",
  checkPermission("role:add"),
  async (req, res, next) => {
    try {
      const result = await permHandler.createRole(req.body);
      res.status(CODE.SUCCESS).json(Response.success(result, "角色创建成功"));
    } catch (err) {
      next(err);
    }
  }
);

// 更新角色 (POST)
router.post(
  "/admin/role/update",
  checkPermission("role:edit"),
  async (req, res, next) => {
    try {
      const { id, ...data } = req.body;
      await permHandler.updateRole(id, data);
      res.status(CODE.SUCCESS).json(Response.success(null, "角色更新成功"));
    } catch (err) {
      next(err);
    }
  }
);

// 删除角色 (POST)
router.post(
  "/admin/role/delete",
  checkPermission("role:delete"),
  async (req, res, next) => {
    try {
      const { id } = req.body;
      await permHandler.deleteRole(id);
      res.status(CODE.SUCCESS).json(Response.success(null, "角色删除成功"));
    } catch (err) {
      next(err);
    }
  }
);

/**
 * =========================
 * 权限管理模块
 * =========================
 */
// 获取权限模块列表 (GET)
router.get("/admin/permission/modules", async (req, res, next) => {
  try {
    const modules = await permHandler.getPermissionModules();
    res.status(CODE.SUCCESS).json(Response.success(modules));
  } catch (err) {
    next(err);
  }
});
/**
 * =========================
 * 权限模块分类管理
 * =========================
 */

// 创建权限模块分类 (POST)
router.post(
  "/admin/permission/module/create",
  checkPermission("permission:edit"),
  async (req, res, next) => {
    try {
      const result = await permHandler.createPermissionModule(req.body);
      res
        .status(CODE.SUCCESS)
        .json(Response.success(result, "模块分类创建成功"));
    } catch (err) {
      next(err);
    }
  }
);

// 更新权限模块分类 (POST)
router.post(
  "/admin/permission/module/update",
  checkPermission("permission:edit"),
  async (req, res, next) => {
    try {
      const { id, ...data } = req.body;
      await permHandler.updatePermissionModule({ id, ...data });
      res.status(CODE.SUCCESS).json(Response.success(null, "模块分类更新成功"));
    } catch (err) {
      next(err);
    }
  }
);

// 删除权限模块分类 (POST)
router.post(
  "/admin/permission/module/delete",
  checkPermission("permission:edit"),
  async (req, res, next) => {
    try {
      const { id } = req.body;
      await permHandler.deletePermissionModule(id);
      res.status(CODE.SUCCESS).json(Response.success(null, "模块分类删除成功"));
    } catch (err) {
      next(err);
    }
  }
);

// 获取权限模块分类详情 (POST)
router.post("/admin/permission/module/detail", async (req, res, next) => {
  try {
    const { id } = req.body;
    const module = await permHandler.getPermissionModuleById(id);
    res.status(CODE.SUCCESS).json(Response.success(module));
  } catch (err) {
    next(err);
  }
});
// 获取权限列表 (GET)
router.get("/admin/permission/list", async (req, res, next) => {
  try {
    const permissions = await permHandler.getPermissionList(req.query);
    res.status(CODE.SUCCESS).json(Response.success(permissions));
  } catch (err) {
    next(err);
  }
});

// 添加权限 (POST)
router.post(
  "/admin/permission/create",
  checkPermission("permission:add"),
  async (req, res, next) => {
    try {
      const result = await permHandler.createPermission(req.body);
      res.status(CODE.SUCCESS).json(Response.success(result, "权限创建成功"));
    } catch (err) {
      next(err);
    }
  }
);

// 更新权限 (POST)
router.post(
  "/admin/permission/update",
  checkPermission("permission:edit"),
  async (req, res, next) => {
    try {
      const { id, FName, FCode, FDescription, FModuleId } = req.body;
      await permHandler.updatePermission(
        id,
        FName,
        FCode,
        FDescription,
        FModuleId
      );
      res.status(CODE.SUCCESS).json(Response.success(null, "权限更新成功"));
    } catch (err) {
      next(err);
    }
  }
);

// 删除权限 (POST)
router.post(
  "/admin/permission/delete",
  checkPermission("permission:delete"),
  async (req, res, next) => {
    try {
      const { id } = req.body;
      await permHandler.deletePermission(id);
      res.status(CODE.SUCCESS).json(Response.success(null, "权限删除成功"));
    } catch (err) {
      next(err);
    }
  }
);

/**
 * =========================
 * 角色权限管理模块
 * =========================
 */

// 获取角色权限 (POST)
router.post(
  "/admin/role/permissions",
  checkPermission("role:edit"),
  async (req, res, next) => {
    try {
      const { roleId } = req.body;
      const permissions = await permHandler.getRolePermissions(roleId);
      res.status(CODE.SUCCESS).json(Response.success(permissions));
    } catch (err) {
      next(err);
    }
  }
);

// 更新角色权限 (POST)
router.post(
  "/admin/role/permissions/update",
  checkPermission("role:edit"),
  async (req, res, next) => {
    try {
      const { FRoleId, FPermissionIds } = req.body;
      await permHandler.updateRolePermissions(FRoleId, FPermissionIds);
      res.status(CODE.SUCCESS).json(Response.success(null, "权限更新成功"));
    } catch (err) {
      next(err);
    }
  }
);

/**
 * =========================
 * 用户角色管理模块
 * =========================
 */

// 获取用户角色 (POST)
router.post(
  "/admin/user/roles",
  checkPermission("user:edit"),
  async (req, res, next) => {
    try {
      const { userId } = req.body;
      const roles = await permHandler.getUserRoles(userId);
      res.status(CODE.SUCCESS).json(Response.success(roles));
    } catch (err) {
      next(err);
    }
  }
);

// 更新用户角色 (POST)
router.post(
  "/admin/user/roles/update",
  checkPermission("user:edit"),
  async (req, res, next) => {
    try {
      const { FUserId, FRoleIds } = req.body;
      await permHandler.updateUserRoles(FUserId, FRoleIds);
      res.status(CODE.SUCCESS).json(Response.success(null, "用户角色更新成功"));
    } catch (err) {
      next(err);
    }
  }
);

/**
 * =========================
 * 用户权限管理模块
 * =========================
 */

// 获取当前用户权限 (GET)
router.get("/admin/user/permissions/current", async (req, res, next) => {
  try {
    const permissions = await permHandler.getUserPermissions(req.user.userId);
    res.status(CODE.SUCCESS).json(Response.success(permissions));
  } catch (err) {
    next(err);
  }
});

module.exports = router;
